
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:hive_ui/boxes_view.dart';
import '../entity/book_entity.dart';
import '../event/index_refresh_event.dart';
import '../main.dart';
import 'distributed_book_page.dart';
import 'setting_common_page.dart';
import 'stack_book_page.dart';



class Setting extends StatefulWidget {
  const Setting({Key? key}) : super(key: key);

  @override
  State<Setting> createState() => _SettingState();
}

class _SettingState extends State<Setting> {

  Map<Box<dynamic>, dynamic Function(dynamic json)> get allBoxes => {
    books: (json) => BookEntity.fromJson(json),
  };

  _title(){
    return Container(
        margin: EdgeInsets.only(left: 12.w,right: 12.w),
        width: double.infinity,
        height: 60.h,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [

            Positioned(
              left: 0,
              child:  InkWell(
                onTap: (){
                  Get.back();
                },
                child: Image.asset("assets/images/back.png",width:80.w, fit: BoxFit.fitWidth,),
              ),
            ),
            Text("管理端",style: TextStyle(color: const Color.fromRGBO(29, 125, 168, 1),fontSize: 16.sp,fontWeight: FontWeight.bold),),
            Positioned(
              right: 30.w,
              child:  InkWell(
                onTap: (){
                  eventFactory.fire(IndexRefreshEvent());

                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => HiveBoxesView(
                        hiveBoxes: allBoxes,
                        onError: (String errorMessage) =>
                        {
                          print(errorMessage)
                        })),
                  );
                },
                child:  Text("数据库",style: TextStyle(color: const Color.fromRGBO(29, 125, 168, 1),fontSize: 12.sp),),
              ),
            ),
            Positioned(
              right: 0,
              child:  InkWell(
                onTap: (){
                  Get.defaultDialog(
                    title: '退出',
                    titleStyle: TextStyle(
                      color: const Color(0xFF000000),
                      fontSize: 10.sp,
                    ),
                    middleText: '确定退出应用？',
                    middleTextStyle: TextStyle(
                      color: const Color(0xFF000000),
                      fontSize: 8.sp,
                    ),
                    radius: 4.sp,
                    textConfirm: '确定',
                    // textCancel: '取消',
                    confirmTextColor: const Color(0xFF007AFF),
                    backgroundColor: const Color(0xD1F8F8F8),
                    buttonColor: Colors.transparent,
                    onConfirm: () {
                      _showBar();
                      SystemNavigator.pop();
                      exit(0);
                    },
                  );
                },
                child:  Text("退出",style: TextStyle(color: Colors.red,fontSize: 12.sp),),
              ),
            ),

          ],
        )
    );
  }

  _showBar() async {
    await const MethodChannel('toAndroid').invokeMethod('showBar');
  }


  _image(name,callback){
    return InkWell(
      onTap:(){
        callback();
      },
      child: Image.asset("assets/images/$name.png",width: 330.w, fit: BoxFit.fitWidth,),
    );
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
         color: Color.fromRGBO(242,254, 255, 1)
        ),
        child: Column(
          children: [
            _title(),
            SizedBox(height: 60.h,),
            _image( "setting1",()=>Get.to(()=> const SettingCommon())),
            SizedBox(height: 10.h,),
           _image( "setting3",()=>Get.to(()=> const DistributedBook())),
            SizedBox(height: 10.h,),
           _image( "setting2",()=>Get.to(()=> const StackBook())),
            Align(child:  Text('v2.1.0',style: TextStyle(color: Colors.white,fontSize: 12.sp)),)
          ],
        ),
      ),
    );
  }

}

